home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12358 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.6 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Is this a C BUG??? (A string issue)
  5. Date: 31 Mar 1996 10:39:11 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4jmjgfINN8pr@keats.ugrad.cs.ubc.ca>
  8. References: <4jknpf$9k3@abel.cc.sunysb.edu>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <4jknpf$9k3@abel.cc.sunysb.edu>,
  12. George Hauser <ghauser@ic.sunysb.edu> wrote:
  13. >I am reading the whole line in one shot using fgets(). I need to pad with 
  14. >blank spaces those records that are < 194, until the string is set to 194.
  15. >
  16. >Sometimes my records are already filled with 194 and sometimes not.
  17. >
  18. >When I get the string of a line that contains 195 chars (data + eoln) it
  19. >returns a line of 193 and then the next line returns 2 characters.... 
  20. >
  21. >This is WRONG fgets should return 194 characters... but it seems like it
  22. >finds a NULL on space 193 and then returns the end of line and another null.
  23.  
  24. No. The 's' in 'fgets' stands for string. Strings must be zero terminated, and
  25. fgets makes sure of that. When you give it a buffer that is 194 long, it can
  26. store a string that is no longer than 193. The _size_ of a string is always one
  27. more than the _length_ of a string to account for the terminating null
  28. character. Declare the array to be of size 195 if you wish to store strings up
  29. to 194 long.
  30.  
  31. >I have spent too much time with this so now even though its ugly I simply
  32. >check to see if the lenght of the string is greater than 2.
  33.  
  34. But is this what you really want to do? It's bad programming to write your
  35. program such that it sets a buffer for the maximum expected line length.
  36.  
  37. What you are doing could be done better if you read the file one character at a
  38. time.
  39.  
  40.     store zero to column count
  41.     loop forever
  42.         get character from input file
  43.         if EOF
  44.             break out of loop
  45.         if newline
  46.             increment record count.
  47.             if column count < 194
  48.                 write 194-count padding spaces to output file 
  49.             store zero to column count
  50.         else if character is not "illegal" and column count <= 194
  51.             write it to output file
  52.             increment column count
  53.     end loop
  54.     if last character was not a newline, increment record count.
  55.  
  56. >Here's the buggy code.
  57.  
  58. Your code is a little bit overkill, sorry to say.
  59.  
  60. When in doubt, process individual characters rather than being clever with
  61. buffers and string manipulation. It can be more efficient and cleaner.
  62. The standard IO library already buffers input for you! When you call
  63. getc(stream), you are typically invoking a fast macro to pull a character out
  64. of stdio's buffer, replenishing that buffer if necessary. 
  65. -- 
  66.  
  67.